//
//  ClaimPerInfoViewController.m
//  MemberProject
//
//  Created by vincent luo on 15/7/17.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "ClaimPerInfoViewController.h"
#import "ClaimPerInfoTableViewCell.h"
#import "ClaimPerInfoTableViewTableViewCell.h"
#import "BuaatVolunteerServerSDK.h"
#import "VolunteerClaimViewController.h"
#import "ClaimBankInfoViewController.h"
#import "ClaimPicViewController.h"
#import "ClaimSuccessViewController.h"
#import "UserInsuranceManager.h"
@interface ClaimPerInfoViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic)UITableView *tableView;
@property(strong,nonatomic)NSArray *data;

@property(strong,nonatomic)NSArray *images;
@end

@implementation ClaimPerInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title=@"自助申请";
    
    NSMutableArray *arrM=[[NSMutableArray alloc]init];
    NSMutableArray *arrMarrM=[NSMutableArray array];
    [arrMarrM addObject:@{@"key":@"姓名:",@"context":@"张军"}];
    [arrMarrM addObject:@{@"key":@"性别:",@"context":@"男"}];
    [arrMarrM addObject:@{@"key":@"证件类型:",@"context":@"身份证"}];
    [arrMarrM addObject:@{@"key":@"证件号码:",@"context":@"440681199220193055"}];
    [arrM addObject:arrMarrM];
    
    arrMarrM=[NSMutableArray array];
    [arrMarrM addObject:@{@"key":@"出险日期:",@"context":@"2014-09-26"}];
    [arrMarrM addObject:@{@"key":@"申请类型:",@"context":@"意外医疗"}];
    [arrMarrM addObject:@{@"key":@"申请人地址:",@"context":@"jdkaljdklajdksljdaklsjdksaljdkasljdkals"}];
    [arrMarrM addObject:@{@"key":@"手机号:",@"context":@"15099879832"}];
    [arrM addObject:arrMarrM];
    
    arrMarrM=[NSMutableArray array];
    [arrMarrM addObject:@{@"key":@"账户名:",@"context":@"张军"}];
    [arrMarrM addObject:@{@"key":@"账号:",@"context":@"328910381903810"}];
    [arrMarrM addObject:@{@"key":@"开户行:",@"context":@"jdkaljdklajdksljdaklsjdksaljdkasljdkals"}];
    [arrMarrM addObject:@{@"key":@"分行:",@"context":@"15099879832"}];
    [arrM addObject:arrMarrM];
    
    arrMarrM=[NSMutableArray array];
    [arrMarrM addObject:@""];
    [arrM addObject:arrMarrM];
    self.data=arrM;
    
    
    
    UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    tableView.delegate=self;
    tableView.dataSource=self;
    [self.view addSubview:tableView];
    self.tableView=tableView;

    [self.tableView registerNib:[UINib nibWithNibName:@"ClaimPerInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"CellIdentifier"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ClaimPerInfoTableViewTableViewCell" bundle:nil] forCellReuseIdentifier:@"CellIdentifierLast"];

    UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(5, (footerView.height-40)/2, SCREEN_WIDTH-2*5, 40);
    [footerView addSubview:button];
    [button setTitle:@"提交" forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"redbar.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(chickSubmitButton:) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.tableFooterView=footerView;
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self refresh];
}

-(void)refresh{
    NSMutableArray *arrM=[[NSMutableArray alloc]init];
    NSMutableArray *arrMarrM=[NSMutableArray array];
    ModelUser *user=[UserInfoManager sharedManager].modelMser;
    
    [arrMarrM addObject:@{@"key":@"姓名:",@"context":user.userName}];
    int gender=[user.gender intValue];
    if(gender==0){
        [arrMarrM addObject:@{@"key":@"性别:",@"context":@""}];
    }else if (gender==1){
        [arrMarrM addObject:@{@"key":@"性别:",@"context":@"男"}];
    }else{
        [arrMarrM addObject:@{@"key":@"性别:",@"context":@"女"}];
    }
    [arrMarrM addObject:@{@"key":@"证件类型:",@"context":@"身份证"}];
    NSString *certificateNo=user.certificateNo;
    certificateNo=[[BuaatVolunteerServerSDK sharedInstance]decryptStrWithPublicKey:certificateNo];
    [arrMarrM addObject:@{@"key":@"证件号码:",@"context":certificateNo}];
    [arrM addObject:arrMarrM];
    
    UserClaimInput *claimInput=[UserInfoManager sharedManager].userClaimInput;
    arrMarrM=[NSMutableArray array];
    if(claimInput.dangerDate.length==0){
        [arrMarrM addObject:@{@"key":@"出险日期:",@"context":@""}];
    }else{
        [arrMarrM addObject:@{@"key":@"出险日期:",@"context":claimInput.dangerDate}];
    }
    [arrMarrM addObject:@{@"key":@"申请类型:",@"context":@"意外医疗"}];
    
    [arrMarrM addObject:@{@"key":@"申请人地址:",@"context":claimInput.contactAddress}];
    [arrMarrM addObject:@{@"key":@"手机号:",@"context":[[BuaatVolunteerServerSDK sharedInstance]decryptStrWithPublicKey:user.loginMobile]}];
    [arrM addObject:arrMarrM];
    
    arrMarrM=[NSMutableArray array];
    if(claimInput.bankAccountName.length==0){
        [arrMarrM addObject:@{@"key":@"账户名:",@"context":@""}];
    }else{
        [arrMarrM addObject:@{@"key":@"账户名:",@"context":claimInput.bankAccountName}];
    }
    [arrMarrM addObject:@{@"key":@"账号:",@"context":claimInput.bankAccount}];
    [arrMarrM addObject:@{@"key":@"开户行:",@"context":claimInput.bankName}];
    if(claimInput.bankBranchName.length==0){
        [arrMarrM addObject:@{@"key":@"分行:",@"context":@""}];
    }else{
        [arrMarrM addObject:@{@"key":@"分行:",@"context":claimInput.bankBranchName}];
    }
    [arrM addObject:arrMarrM];
    
    arrMarrM=[NSMutableArray array];
    [arrMarrM addObject:@""];
    [arrM addObject:arrMarrM];
    self.data=arrM;
    
    self.images=claimInput.images;
    
    [self.tableView reloadData];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"ClaimPerInfoUpdate" object:nil];
}

-(void)chickSubmitButton:(UIButton *)sender{
    NSArray *nameArr=@[@"医疗费用原始凭证(必填)",@"被保险人身份证明(必填)",@"门(急)诊病例(必填)",@"住院病例(出院小结)",@"医疗费用明细清单_处方",@"病理_血液_影像检查报告",@"意外事故证明"];
    [SVProgressHUD showWithStatus:@"提交中" maskType:SVProgressHUDMaskTypeClear];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSMutableArray *arrM=[NSMutableArray array];
        for(int i=0;i<self.images.count;i++){
            NSMutableArray *arrMarrM=[NSMutableArray array];
            NSArray *arr=self.images[i];
            NSString *name=nameArr[i];
            for(int j=0;j<arr.count;j++){
                NSData *data=UIImageJPEGRepresentation(arr[j], 0.4);
                data = [data base64EncodedDataWithOptions:0];
                NSString *outStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                
                NSString *imgName=[NSString stringWithFormat:@"%@%d.jpeg",name,j];
                [arrMarrM addObject:@{@"img":outStr,@"imgName":imgName}];
            }
            [arrM addObject:arrMarrM];
        }
        
        
        UserClaimInput *claimInput=[UserInfoManager sharedManager].userClaimInput;
        ModelInsurance *insurance = [UserInsuranceManager sharedManager].userInsurance;
        
        NSString *danger =[Utils changeDateToService:claimInput.dangerDate];
        
        if(self.isUpdate==YES){
            [[BuaatVolunteerServerSDK sharedInstance]requestInsuranceUpdateClaimWithClaimId:claimInput.claimId insuranceId:insurance.insuranceId recordId:claimInput.recordId dangerDate:danger bankAccountName:claimInput.bankAccountName bankAccount:claimInput.bankAccount bankAccountCity:claimInput.bankAccountCity bankName:claimInput.bankName bankBranchName:claimInput.bankBranchName bankSubBranchName:claimInput.bankSubBranchName contactAddress:claimInput.contactAddress applyCost:claimInput.applyCost medicalPayList:arrM[0] identificationList:arrM[1] medicalRecordList:arrM[2] inHospitalList:arrM[3] medicalExpenseList:arrM[4] checkReportList:arrM[5] accidentList:arrM[6] success:^(NSHTTPURLResponse *response, id responseObject) {
                if([responseObject[@"ret"] intValue]==0){
                    [SVProgressHUD dismiss];
                    ClaimSuccessViewController *vc=[[ClaimSuccessViewController alloc]init];
                    [self.navigationController pushViewController:vc animated:YES];
                }else{
                    [SVProgressHUD showErrorWithStatus:@"操作错误"];
                }
            } failure:^(NSHTTPURLResponse *response, NSError *error) {
                [SVProgressHUD showErrorWithStatus:@"网络超时"];
            }];
        }else{
            [[BuaatVolunteerServerSDK sharedInstance]requestVolunteerClaimInsuranceWithInsuranceId:insurance.insuranceId recordId:claimInput.recordId dangerDate:danger bankAccountName:claimInput.bankAccountName bankAccount:claimInput.bankAccount bankAccountCity:claimInput.bankAccountCity bankName:claimInput.bankName bankBranchName:claimInput.bankBranchName bankSubBranchName:claimInput.bankSubBranchName contactAddress:claimInput.contactAddress applyCost:claimInput.applyCost medicalPayList:arrM[0] identificationList:arrM[1] medicalRecordList:arrM[2] inHospitalList:arrM[3] medicalExpenseList:arrM[4] checkReportList:arrM[5] accidentList:arrM[6] success:^(NSHTTPURLResponse *response, id responseObject) {
                if([responseObject[@"ret"] intValue]==0){
                    [SVProgressHUD dismiss];
                    ClaimSuccessViewController *vc=[[ClaimSuccessViewController alloc]init];
                    [self.navigationController pushViewController:vc animated:YES];
                }else{
                    [SVProgressHUD showErrorWithStatus:@"操作错误"];
                }
            } failure:^(NSHTTPURLResponse *response, NSError *error) {
                [SVProgressHUD showErrorWithStatus:@"网络超时"];
            }];
        }
    });
}


#pragma mark - tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.data.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    /*
    [arrMarrM addObject:@{@"key":@"姓名:",@"context":@"张军"}];
    [arrMarrM addObject:@{@"key":@"性别:",@"context":@"男"}];
    [arrMarrM addObject:@{@"key":@"证件类型:",@"context":@"身份证"}];
    [arrMarrM addObject:@{@"key":@"证件号码:",@"context":@"440681199220193055"}];
    [arrM addObject:arrMarrM];
    
    arrMarrM=[NSMutableArray array];
    [arrMarrM addObject:@{@"key":@"出生日期:",@"context":@"2014-09-26"}];
    [arrMarrM addObject:@{@"key":@"申请类型:",@"context":@"意外医疗"}];
    [arrMarrM addObject:@{@"key":@"申请人地址:",@"context":@"jdkaljdklajdksljdaklsjdksaljdkasljdkals"}];
    [arrMarrM addObject:@{@"key":@"手机号:",@"context":@"15099879832"}];
    [arrM addObject:arrMarrM];
    
    arrMarrM=[NSMutableArray array];
    [arrMarrM addObject:@{@"key":@"账户名:",@"context":@"张军"}];
    [arrMarrM addObject:@{@"key":@"账号:",@"context":@"328910381903810"}];
    [arrMarrM addObject:@{@"key":@"开户行:",@"context":@"jdkaljdklajdksljdaklsjdksaljdkasljdkals"}];
    [arrMarrM addObject:@{@"key":@"分行:",@"context":@"15099879832"}];
    [arrMarrM addObject:@{@"key":@"支行:",@"context":@"15099879832"}];
*/
    
    
    NSArray *arr=self.data[section];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier=@"CellIdentifier";
    static NSString *CellIdentifierLast=@"CellIdentifierLast";

    if(indexPath.section==3){
        ClaimPerInfoTableViewTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifierLast forIndexPath:indexPath];
        cell.data=self.images;
        return cell;
    }else{
        ClaimPerInfoTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        NSArray *arr=self.data[indexPath.section];
        NSDictionary *d=arr[indexPath.row];
        cell.keyLabel.text=d[@"key"];
        cell.contextLabel.text=d[@"context"];
        return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==3){
        return 109*self.images.count;
    }
    float readyHeight=40.0f;
    NSArray *arr=self.data[indexPath.section];
    NSDictionary *d=arr[indexPath.row];
    float tempHeight=[Utils heightForLabel:d[@"context"] andFont:15.0f andWidth:SCREEN_WIDTH-115];
    tempHeight+=4;
    if(tempHeight<readyHeight){
        tempHeight=readyHeight;
    }
    return tempHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    view.backgroundColor=[UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1];
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, (view.height-23)/2, 120, 23)];
    [view addSubview:imageView];
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(40, 0, 200, view.height)];
    [view addSubview:label];
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(SCREEN_WIDTH-20-5, (view.height-20)/2, 20, 20);
    button.tag=100+section;
    [view addSubview:button];
//    button.backgroundColor=[UIColor redColor];
    [button setImage:[UIImage imageNamed:@"edit"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(chickEditButton:) forControlEvents:UIControlEventTouchUpInside];
    switch (section) {
        case 0:{
            imageView.image=[UIImage imageNamed:@"ios_07"];
            label.text=@"申请人信息";
            button.hidden=YES;
        }
            break;
        case 1:{
            imageView.image=[UIImage imageNamed:@"ios_17"];
            label.text=@"出险信息";

        }
            break;
        case 2:{
            imageView.image=[UIImage imageNamed:@"ios_33"];
            label.text=@"银行信息";

        }
            break;
        case 3:{
            imageView.image=[UIImage imageNamed:@"ios_41"];
            label.text=@"影像资料";

        }
            break;
        default:
            break;
    }
    
    
    
    return view;
}

-(void)chickEditButton:(UIButton *)sender{
    int tag=sender.tag-100;
    switch (tag) {
        case 1:{
            //出险信息
            VolunteerClaimViewController *vc=[[VolunteerClaimViewController alloc]init];
            vc.isChangeInfo=YES;
            BaseNavigationController *nav=[[BaseNavigationController alloc]initWithRootViewController:vc];
            [self presentViewController:nav animated:YES completion:nil];
        }
            break;
        case 2:{
            //银行信息
            ClaimBankInfoViewController *vc=[[ClaimBankInfoViewController alloc]init];
            vc.isChangeInfo=YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3:{
            //图片
            if(self.isUpdate==YES){
                ClaimPicViewController *vc=[[ClaimPicViewController alloc]init];
                vc.isUpdate=YES;
                vc.images=[UserInfoManager sharedManager].userClaimInput.images;
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
            break;
    }
    
    
    
    }

@end
